import { Component } from '@angular/core';
import {
    AlertController,
    LoadingController,
    NavController,
    NavParams,
    Platform,
    ToastController
} from 'ionic-angular';
import { BasePage } from "../../BasePage";
import { UserManager } from "../../../framework/manager/UserManager";
import { SystemService } from "../../service/SystemService";
import { Log } from "../../../framework/utils/Log";
import { ExamineApprovePage } from "../../examine/examineApprove";
import {ZxInformationPage} from "../../examineApprove/zxInformation/zxInformation";
import {ZjInformationPage} from "../../examineApprove/zjInformation/zjInformation";
import {HtbgInformationPage} from "../../examineApprove/htbgInformation/htbgInformation";
import {HtqdInformationPage} from "../../examineApprove/htqdInformation/htqdInformation";
import {CgInformationPage} from "../../examineApprove/cgInformation/cgInformation";

/**
 * Generated class for the ToDoApprovalPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-to-do-approval',
    templateUrl: 'to-do-approval.html',
})
export class ToDoApprovalPage extends BasePage {

    private static TAG: string = 'ToDoApprovalPage';
    private _userId: string;
    private _pageNum: number = 1;
    private _rowNum: number = Math.ceil(window.innerHeight / 60);
    private isEnd: boolean = false;
    dataList: any = [];
    type:string="1";
    constructor(public navCtrl: NavController, public navParams: NavParams, public sys: SystemService, public platform: Platform,
        public loadingCtrl: LoadingController,
        public toastCtrl: ToastController,
        public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this._userId = UserManager.getInstance().user.userId;
    }

    ionViewDidLoad() {
        Log.w(ToDoApprovalPage.TAG, 'ionViewDidLoad ToDoApprovalPage');
        //this.getApprovalList();
    }
    ionViewWillEnter(){
        Log.w(ToDoApprovalPage.TAG, 'ionViewWillEnter ToDoApprovalPage');
        this.dataList=[];
        this._pageNum = 1;
        this.isEnd = false;
        this.getApprovalList();
    }
    //获取审批列表
    getApprovalList() {
        this.showLoading(true, '加载中');
        let year = UserManager.getInstance().user.year;
        let data: any = {
            NianDu: year,
            YongHuBH: this._userId,
            PageNum: this._pageNum,
            RowNum: this._rowNum,
            LeiXing: this.type
        };
        this.sys.getToDoApprova(data).subscribe((res: any) => {
            Log.printObj(ToDoApprovalPage.TAG, 'printObj==', res);
            if (res.msgCode == '1') {
                this.dataList = res.list;
            } else {
                //this.isEnd = true;
            }
            this.showLoading(false, '');
        }, error => {
            this.showLoading(false, '');
            this.showError('数据请求失败');
        })
    }

    notifyHandler(item) {

        if(item.DanJuLeiXing=="1"){
            this.navCtrl.push(ExamineApprovePage, { data: item, type:this.type });
        }else if(item.DanJuLeiXing=="2"){//执行申请
            this.getCheckDetail(item,"2");
        }else if(item.DanJuLeiXing=="3"){//资金申请
            this.getZjCheckDetail(item,"3");
        } else if(item.DanJuLeiXing=="6"){//合同变更
            this.getHtgldbspBgdetail(item,"6");
        }else if(item.DanJuLeiXing=="5"){//合同签订
            this.getHtgldHbspDetail(item,"5");
        }else if(item.DanJuLeiXing=="4"){//采购申请
            this.getCgxqdbspDetail(item,"4");
        }
    }
    /**
    * 审批详情(合同变更)
    * */
    getHtgldbspBgdetail(_d,spType){
        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD
        };
        this.sys.htgldbspBgdetail(data).subscribe((res: any) => {
            Log.printObj(ToDoApprovalPage.TAG, '审批详情printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(HtbgInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    /**
     * 审批详情(合同签订)
     * */
    getHtgldHbspDetail(_d,spType){
        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD
        };
        this.sys.htgldbspDetail(data).subscribe((res: any) => {
            Log.printObj(ToDoApprovalPage.TAG, '审批详情(合同签订)printObj==', res);
            if (res.msgCode === '1') {
                this.navCtrl.push(HtqdInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    /**
     * 审批详情(采购需求)
     * */
    getCgxqdbspDetail(_d,spType){

        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD

        };
        this.sys.cgxqdbspDetail(data).subscribe((res: any) => {
            Log.printObj(ToDoApprovalPage.TAG, '审批详情(采购需求)printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(CgInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    getZjCheckDetail(_d,spType) {
        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD
        };
        this.sys.dbspzjDetails(data).subscribe((res: any) => {
            Log.printObj(ToDoApprovalPage.TAG, '资金待审详细printObj==', res);
            if (res.msgCode == '1') {
                this.navCtrl.push(ZjInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }
        }, error => {
            this.showError('数据请求失败');
        })
    }
    //执行申请
     getCheckDetail(_d,spType) {
        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD
        };
         this.sys.dbspzxDetails(data).subscribe((res: any) => {
            if (res.msgCode == '1') {
                //this.danJuInfo =this.detailInfo.DanJuXX.ShenQingDH;
                Log.printObj(ToDoApprovalPage.TAG, "执行待审详细data==",res);
                this.navCtrl.push(ZxInformationPage, { data: res,type:this.type,_d:data,spType:spType})
            } else {
                this.showError('数据请求失败');
            }

        }, error => {
            this.showError('数据请求失败');
        })
    }
    doInfinite(infiniteScroll) {
        let year = UserManager.getInstance().user.year;

        console.log('Begin infiniteScroll async operation');
       // this.showLoading(true, '请稍后');
        setTimeout(() => {
            this._pageNum++;
            let data: any = {
                NianDu: year,
                YongHuBH: this._userId,
                PageNum: this._pageNum,
                RowNum: this._rowNum,
                LeiXing: this.type
            };
            this.sys.getToDoApprova(data).subscribe((res: any) => {
                if (res.msgCode == '1' && res.list && res.list.length > 0) {
                    this.dataList.push(...res.list);
                    infiniteScroll.complete();
                } else {
                    this.isEnd = true;
                  //  this.showError("已加载所有");
                }
                this.showLoading(false, '请稍后');
            }, error => {
                this.showLoading(false, '请稍后');
                this.showError('数据请求失败')
            });
            console.log('Async infiniteScroll operation has ended');
            if (this.isEnd) {
                infiniteScroll.enable(false);
               // this.showError("已加载所有");
            }
        }, 500);


    }

    doRefresh(refresher) {
        console.log('Begin refresher async operation');
        setTimeout(() => {
            this.isEnd=false;
            this._pageNum = 1;
            this.dataList=[];
            this.getApprovalList();
            console.log('Async refresher operation has ended');
            refresher.complete();
        }, 500);
    }
    segmentChanged(ev){
        this.isEnd=false;
        this.dataList=[];
        this._pageNum = 1;
        console.log('Segment changed', ev.value);
        this.type = ev.value;
        this.getApprovalList();
    }
}
